<?php
require_once("../../config/config.inc.php");
require_once("task_function.php"); 
require_once($CFG_REAL_CLASSES."/json.class.php");
$json = new Services_JSON();

$sUserID   = $SEC->sUserID;
$sUsername = $SEC->sUserName;
$sRole     = $SEC->sRole;
$sAction   = fn_getParam("strAction","");
$sProjectID   = $_SESSION["task_project_id"];
$sTaskEditAll = fn_isNull($_SESSION["task_enable_editall"],"none"); //editall

if ($sAction == 'treelist') {
    $sParentID = fn_getParam('node',"0");
    //task list
    $aTaskList = array();
    $sql  = "SELECT ID,PARENTID,ORDERNUMB,TASKNAME FROM $TABLE_TASK ";
    $sql .= "WHERE PROJECTID='".$sProjectID."' AND PARENTID='".$sParentID."' ";
    $sql .= "ORDER BY ORDERNUMB";
    $stmt = $DB->query($sql);
    while ($row = $DB->fetchArray($stmt)) {
        $sql2 = "SELECT COUNT(ID) FROM $TABLE_TASK ";
        $sql2.= "WHERE PROJECTID='".$sProjectID."' AND PARENTID='".$row["ID"]."'";
        if ($DB->getFirstField($sql2) > 0) {
            $aTaskList[] = array('text'=>$row["ORDERNUMB"].":".$row["TASKNAME"],'id'=>$row["ID"],'pid'=>$row["PARENTID"],'cls'=>'folder');
        } else {
            $aTaskList[] = array('text'=>$row["ORDERNUMB"].":".$row["TASKNAME"],'id'=>$row["ID"],'pid'=>$row["PARENTID"],'leaf'=>true,'cls'=>'file');
        }
    }
    $DB->freeResult($stmt);

    //echo json_encode($nodes); //use in PHP5 version
    echo $json->encode($aTaskList);
    return;
}
else if ($sAction == 'preTaskList') {
    $sTaskID = fn_getParam('strTaskID',0);
    $sID = fn_getParam('strID',0);
    $aPreTaskType   = $DICT->getDictArray("BASE_PRETASK_TYPE",false);

    $sql  = "SELECT A.ID,A.PRETASKID,B.ORDERNUMB,B.TASKNAME,A.PRETASKTYPE,A.STAYTIME,A.REMARK ";
    $sql .= "FROM $TABLE_PRETASK A,$TABLE_TASK B ";
    $sql .= "WHERE A.PRETASKID=B.ID";
    if ($sTaskID > 0) {
        $sql .= " AND A.TASKID=".$sTaskID;
    }
    if ($sID > 0) {
        $sql .= " AND A.ID=".$sID;
    }
    $sql .= " ORDER BY B.ORDERNUMB";
    $stmt = $DB->query($sql);
    $aData = array();
    while ($row = $DB->fetchArray($stmt)) {
        $aData[count($aData)] = array('strID'=>$row['ID'],'strPreTaskID'=>$row['PRETASKID'],
            'strTaskName'=>$row['ORDERNUMB'].":".$row['TASKNAME'],
            'strPreType'=>$row['PRETASKTYPE'],'strPreTaskType'=>$aPreTaskType[$row['PRETASKTYPE']],
            'strStayTime'=>$row['STAYTIME'],'strRemark'=>$row['REMARK']);
    }

    $DB->freeResult($stmt);
    echo $json->encode(array('success'=>true,'total'=>count($aData),'data'=>$aData));
    return;
}
else if ($sAction == 'resourceList') {
    $sTaskID = fn_getParam('strTaskID',0);
    $sID = fn_getParam('strID',0); 

    $sql  = "SELECT ID,USERNAME,TRUENAME,FORMAT(USAGEUNIT,0) AS USAGEUNIT,REMARK ";
    $sql .= "FROM $TABLE_TASK_RESOURCE ";
    $sql .= "WHERE 1=1";
    if ($sTaskID > 0) {
        $sql .= " AND TASKID=".$sTaskID;
    }
    if ($sID > 0) {
        $sql .= " AND ID=".$sID;
    }
    $sql .= " ORDER BY USERNAME";
    $stmt = $DB->query($sql);
    $aData = array();
    while ($row = $DB->fetchArray($stmt)) {
        $aData[count($aData)] = array('strID'=>$row['ID'],'strUserName'=>$row['USERNAME'],'strTrueName'=>$row['TRUENAME'],
            'strUsageUnit'=>$row['USAGEUNIT'],'strRemark'=>$row['REMARK']);
    }

    $DB->freeResult($stmt);
    echo $json->encode(array('success'=>true,'total'=>count($aData),'data'=>$aData));
    return;
}
else if ($sAction == "preTaskNameList") {
    $sOrderNumb = fn_getParam("strOrderNumb","0");
    $aPreTask = array();
    $sql  = "SELECT ID,ORDERNUMB,TASKNAME FROM $TABLE_TASK ";
    $sql .= "WHERE PROJECTID='".$sProjectID."' ";
    $sql .= "ORDER BY ORDERNUMB";
    $stmt = $DB->query($sql);
    while ($row = $DB->fetchArray($stmt)) {
        if ($row["ORDERNUMB"] !== $sOrderNumb) {
            $aPreTask[] = array('sValue'=>$row["ID"] ,'sText'=>$row["ORDERNUMB"].":".$row["TASKNAME"]);
        }
    }
    $DB->freeResult($stmt);

    $rset = array('success'=>true,'data'=>$aPreTask);
    echo $json->encode($rset);
    return;
}
else if ($sAction == "resNameList") {
    $aUsername = array();
    $sql = "SELECT `friendid`, `friendusername` FROM ".UC_DBTABLEPRE."friends ";
    $sql.= "WHERE `uid`='".$SEC->sUserID."' ";
    $sql.= "ORDER BY `friendusername`";
    $stmt= $DB->query($sql);
    while ($row = $DB->fetchRow($stmt)) {
        $aUsername[] = array('sValue'=>$row["friendusername"] ,'sText'=>$row["friendusername"]);
    }
    $DB->freeResult($stmt);

    $aUsername[] = array('sValue'=>"task_res_other" ,'sText'=>"其他(手工填写)");
    $rset = array('success'=>true,'data'=>$aUsername);
    echo $json->encode($rset);
    return;
}
if ($sAction == ACTION_VIEW) {
    $sTaskID = fn_getParam('strTaskID',0);
    $aTask = array();

    $sql  = "SELECT ID,USERID,USERNAME,PROJECTID,ORDERNUMB,PARENTID,WBSCODE,TASKNAME,TASKINFO,PRIORITY,DURATION,ISESTIMATE,";
    $sql .= "DATE_FORMAT(PLANSTARTDATE,'%Y-%m-%d') AS PSTARTDATE,DATE_FORMAT(PLANSTARTDATE,'%H:%i') AS PSTARTTIME,";
    $sql .= "DATE_FORMAT(PLANFINISHDATE,'%Y-%m-%d') AS PFINISHDATE,DATE_FORMAT(PLANFINISHDATE,'%H:%i') AS PFINISHTIME,";
    $sql .= "DATE_FORMAT(FACTSTARTDATE,'%Y-%m-%d') AS FSTARTDATE,DATE_FORMAT(FACTSTARTDATE,'%H:%i') AS FSTARTTIME,";
    $sql .= "DATE_FORMAT(FACTFINISHDATE,'%Y-%m-%d') AS FFINISHDATE,DATE_FORMAT(FACTFINISHDATE,'%H:%i') AS FFINISHTIME,";
    $sql .= "DATE_FORMAT(LIMITDATE,'%Y-%m-%d') AS LIMITDATE,DATE_FORMAT(LIMITDATE,'%H:%i') AS LIMITTIME,";
    $sql .= "DATE_FORMAT(CREATEDATE,'%Y-%m-%d %H:%i:%s') AS CREATEDATE,";
    $sql .= "DATE_FORMAT(UPDATEDATE,'%Y-%m-%d %H:%i:%s') AS UPDATEDATE,";
    $sql .= "FINISHPERCENT,FINISHINFO,LIMITTYPE,TASKTYPE,ISMILESTONE,REMARK ";
    $sql .= "FROM $TABLE_TASK WHERE ID='".$sTaskID."'";
    $stmt = $DB->query($sql);
    if ($row = $DB->fetchArray($stmt)) {
        $aTask["strTaskID"]        = $row["ID"];
        $aTask["strDuration"]      = $row["DURATION"];
        $aTask["strIsEstimate"]    = $row["ISESTIMATE"];
        $aTask["strIsMilestone"]   = $row["ISMILESTONE"];
        $aTask["strOrderNumb"]     = $row["ORDERNUMB"];
        $aTask["strPriority"]      = $row["PRIORITY"];
        $aTask["strTaskType"]      = $row["TASKTYPE"];
        $aTask["strLimitType"]     = $row["LIMITTYPE"];
        $aTask["strFinishPercent"] = floor($row["FINISHPERCENT"]);
        $aTask["strWbsCode"]       = fn_setParam($row["WBSCODE"]);
        $aTask["strTaskName"]      = fn_setParam($row["TASKNAME"]);
        $aTask["strTaskInfo"]      = $row["TASKINFO"];
        $aTask["strFinishInfo"]    = $row["FINISHINFO"];
        $aTask["strPlanStartDate"] = fn_fmDatetime($row["PSTARTDATE"] ,"");
        $aTask["strPlanStartTime"] = fn_fmDatetime($row["PSTARTTIME"] ,"");
        $aTask["strPlanFinishDate"]= fn_fmDatetime($row["PFINISHDATE"],"");
        $aTask["strPlanFinishTime"]= fn_fmDatetime($row["PFINISHTIME"],"");
        $aTask["strFactStartDate"] = fn_fmDatetime($row["FSTARTDATE"] ,"");
        $aTask["strFactStartTime"] = fn_fmDatetime($row["FSTARTTIME"] ,"");
        $aTask["strFactFinishDate"]= fn_fmDatetime($row["FFINISHDATE"],"");
        $aTask["strFactFinishTime"]= fn_fmDatetime($row["FFINISHTIME"],"");
        $aTask["strLimitDate"]     = fn_fmDatetime($row["LIMITDATE"],"");
        $aTask["strLimitTime"]     = fn_fmDatetime($row["LIMITTIME"],"");
        $aTask["strCreateUser"]    = $row["USERNAME"];
        $aTask["strCreateDate"]    = $row["CREATEDATE"];
        $aTask["strUpdateDate"]    = $row["UPDATEDATE"];
    }
    $DB->freeResult($stmt);

    $sql  = "SELECT COUNT(USERNAME) FROM $TABLE_TASK_RESOURCE ";
    $sql .= "WHERE TASKID='".$sTaskID."' AND USERNAME='".$sUsername."'";
    $rightflag = $DB->getFirstField($sql);
    echo $json->encode(array('success'=>true,'total'=>"1",'data'=>$aTask,'right'=>$rightflag));
    return;
}

if ($sAction==ACTION_INSERT || $sAction==ACTION_UPDATE || $sAction=="UPDATE_FINSH") {
    $sTaskID        = fn_getParam("strTaskID","");
    $sIsLeaf        = fn_getParam("strIsLeaf","doc");
    $sOrderNumb     = fn_getParam("strOrderNumb","");
    $sOldOrderNumb  = fn_getParam("strOldOrderNumb","");
    $sTaskWhere     = fn_getParam("strTaskWhere","");
    $sTaskName      = fn_getParam("strTaskName","");
    $sDuration      = fn_getParam("strDuration","0.0");
    $sIsEstimate    = fn_getParam("strIsEstimate","0");
    $sTaskType      = fn_getParam("strTaskType","");
    $sPlanStartDate = fn_getParam("strPlanStartDate","0000-00-00");
    $sPlanStartTime = fn_getParam("strPlanStartTime","");
    $sPlanFinishDate= fn_getParam("strPlanFinishDate","0000-00-00");
    $sPlanFinishTime= fn_getParam("strPlanFinishTime","");
    $sLimitType     = fn_getParam("strLimitType","");
    $sLimitDate     = fn_getParam("strLimitDate","0000-00-00");
    $sLimitTime     = fn_getParam("strLimitTime","");
    $sTaskInfo      = fn_getParam("strTaskInfo","");
    $sPriority      = fn_getParam("strPriority","");
    $sFactStartDate = fn_getParam("strFactStartDate","0000-00-00");
    $sFactStartTime = fn_getParam("strFactStartTime","");
    $sFactFinishDate= fn_getParam("strFactFinishDate","0000-00-00");
    $sFactFinishTime= fn_getParam("strFactFinishTime","");
    $sFinishPercent = fn_getParam("strFinishPercent","");
    $sFinishInfo    = fn_getParam("strFinishInfo","");
    $sIsMilestone   = fn_getParam("strIsMilestone","0");
    $sWbsCode       = fn_getParam("strWbsCode","");
    $sPretaskList   = fn_getParam("strPretaskList","");
    $sResourceList  = fn_getParam("strResourceList","");

    if ($sPlanStartDate!="0000-00-00" && $sPlanStartTime!="" && $sPlanStartTime!="00:00") {
        $sPlanStartDate .= " ".$sPlanStartTime;
    }
    if ($sPlanFinishDate!="0000-00-00" && $sPlanFinishTime!="" && $sPlanFinishTime!="00:00") {
        $sPlanFinishDate .= " ".$sPlanFinishTime;
    }
    if ($sFactStartDate!="0000-00-00" && $sFactStartTime!="" && $sFactStartTime!="00:00") {
        $sFactStartDate .= " ".$sFactStartTime;
    }
    if ($sFactFinishDate!="0000-00-00" && $sFactFinishTime!="" && $sFactFinishTime!="00:00") {
        $sFactFinishDate .= " ".$sFactFinishTime;
    }
    if ($sLimitDate!="0000-00-00" && $sLimitTime!="" && $sLimitTime!="00:00") {
        $sLimitDate .= " ".$sLimitTime;
    }
    
    if ($sAction==ACTION_UPDATE || $sAction=="UPDATE_FINSH") {
        $sql = "SELECT PARENTID,PLANSTARTDATE,PLANFINISHDATE,FINISHPERCENT ";
        $sql.= "FROM $TABLE_TASK WHERE ID='".$sTaskID."'";
        $stmt = $DB->query($sql);
        if ($row = $DB->fetchArray($stmt)) {
            $parentID2       = $row["PARENTID"];
            $planStartDate2  = $row["PLANSTARTDATE"];
            $planFinishDate2 = $row["PLANFINISHDATE"];
            $finishPercent2  = $row["FINISHPERCENT"];
        }
        $DB->freeResult($stmt);
    }
}

$conn = $DB->tconnect();
$sResult = "";

if ($sAction=="UPDATE_FINSH") {
    $DB->tbegin($conn);
    $sResult = "任务完成情况保存失败";

    $sql = "UPDATE $TABLE_TASK SET FACTSTARTDATE='".$sFactStartDate;
    $sql.= "',FACTFINISHDATE='".$sFactFinishDate."',FINISHPERCENT='".$sFinishPercent;
    $sql.= "',FINISHINFO='".$sFinishInfo."',UPDATEDATE=now() ";
    $sql.= "WHERE ID='".$sTaskID."'";
    $DB->tquery($sql,$conn);
    
    if ($sFinishPercent != $finishPercent2) {
        autoAdjustPrecis($parentID2,$conn);
    }
    $DB->tcommit($conn);
    $sResult = "任务完成情况保存成功";
    echo $json->encode(array('success'=>true,'msg'=>$sResult));
    return;
} 

if ($sTaskEditAll != "editall") {
    echo $json->encode(array('success'=>false,'msg'=>'no rights'));
    return;
}

if ($sAction == ACTION_UPDATE) {
    $DB->tbegin($conn);
    $sResult = "您选择的任务保存失败";

    $sql = "UPDATE $TABLE_TASK SET TASKNAME='".$sTaskName."',WBSCODE='".$sWbsCode."',TASKINFO='".$sTaskInfo;
    $sql.= "',PRIORITY='".$sPriority."',DURATION='".$sDuration."',PLANSTARTDATE='".$sPlanStartDate;
    $sql.= "',PLANFINISHDATE='".$sPlanFinishDate."',FACTSTARTDATE='".$sFactStartDate;
    $sql.= "',FACTFINISHDATE='".$sFactFinishDate."',FINISHPERCENT='".$sFinishPercent;
    $sql.= "',FINISHINFO='".$sFinishInfo."',LIMITTYPE='".$sLimitType."',LIMITDATE='".$sLimitDate;
    $sql.= "',TASKTYPE='".$sTaskType."',ISMILESTONE='".$sIsMilestone."',ISESTIMATE='".$sIsEstimate;
    $sql.= "' WHERE ID='".$sTaskID."'";
    $DB->tquery($sql,$conn);

    if ($sFinishPercent != $finishPercent2 || 
        $sPlanStartDate != $planStartDate2 ||
        $sPlanFinishDate!= $planFinishDate2) {
        autoAdjustPrecis($parentID2,$conn);
    }

    $DB->tcommit($conn);
    $sResult = "您选择的任务保存成功";
    echo $json->encode(array('success'=>true,'msg'=>$sResult,'taskID'=>""));
} 
else if ($sAction == ACTION_INSERT) {
    $DB->tbegin($conn);
    $sResult = "新任务添加失败";

    if ($sTaskWhere=="last" || $sOldOrderNumb=="") {  //add task in last OrderNumb
        $sql2 = "SELECT MAX(ORDERNUMB) FROM $TABLE_TASK WHERE PROJECTID='".$sProjectID."'";
        $sOldOrderNumb = $DB->getFirstField($sql2);
        if ($sOldOrderNumb > 0) {
            $sql1 = "SELECT PARENTID FROM $TABLE_TASK WHERE PROJECTID='".$sProjectID."' AND ORDERNUMB='".$sOldOrderNumb."'";
            $sNewParentID = $DB->getFirstField($sql1);
            $sOrderNumb = $sOldOrderNumb + 1;
        } else {
            $sNewParentID = '0';
            $sOrderNumb = 1;
        }
    } else if ($sIsLeaf=="doc" && $sOldOrderNumb!="") { 
        $sql = "UPDATE $TABLE_TASK SET ORDERNUMB=ORDERNUMB + 1 ";
        $sql.= "WHERE PROJECTID='".$sProjectID."' ";
        if ($sTaskWhere == "before") {
            $sql.= "AND ORDERNUMB>='".$sOldOrderNumb."'"; 
            $sOrderNumb = $sOldOrderNumb;
        } else if ($sTaskWhere == "after" || $sTaskWhere == "inside") {
            $sql.= "AND ORDERNUMB>'".$sOldOrderNumb."'";
            $sOrderNumb = $sOldOrderNumb + 1;
        }
        if ($sTaskWhere == "inside") {
            $sNewParentID = $sTaskID;
        } else {
            $sql1 = "SELECT PARENTID FROM $TABLE_TASK WHERE ID='".$sTaskID."'";
            $sNewParentID = $DB->getFirstField($sql1);
        }
        $DB->tquery($sql,$conn);
    } else if ($sOldOrderNumb!="") { //else is folder
        $sql = "UPDATE $TABLE_TASK SET ORDERNUMB=ORDERNUMB + 1 ";
        $sql.= "WHERE PROJECTID='".$sProjectID."' ";
        if ($sTaskWhere == "before") {
            $sql.= "AND ORDERNUMB>='".$sOldOrderNumb."'";
            $sOrderNumb = $sOldOrderNumb;
            $sql1 = "SELECT PARENTID FROM $TABLE_TASK WHERE ID='".$sTaskID."'";
            $sNewParentID = $DB->getFirstField($sql1);
        } else if ($sTaskWhere == "inside") {
            $sql.= "AND ORDERNUMB>'".$sOldOrderNumb."'";
            $sOrderNumb = $sOldOrderNumb + 1;
            $sNewParentID = $sTaskID;
        } else if ($sTaskWhere == "after") {
            $sql1 = "SELECT PARENTID FROM $TABLE_TASK WHERE ID='".$sTaskID."'";
            $sNewParentID = $DB->getFirstField($sql1);

            $sql2 = "SELECT MIN(ORDERNUMB) FROM $TABLE_TASK WHERE PROJECTID='".$sProjectID."' ";
            $sql2.= "AND PARENTID='".$sNewParentID."' AND ORDERNUMB>'".$sOldOrderNumb."'";
            $sOrderNumb = $DB->getFirstField($sql2);
            if ($sOrderNumb=="0" || $sOrderNumb=="") {
                $sql2 = "SELECT MAX(ORDERNUMB) FROM $TABLE_TASK WHERE PROJECTID='".$sProjectID."'";
                $sOrderNumb = $DB->getFirstField($sql2);
            }
            $sql.= "AND ORDERNUMB>='".$sOrderNumb."'";
        }
        $DB->tquery($sql,$conn);
    }
    
    $sql = "INSERT INTO $TABLE_TASK(USERID,USERNAME,PROJECTID,ORDERNUMB,PARENTID,WBSCODE,TASKNAME,";
    $sql.= "TASKINFO,PRIORITY,DURATION,ISESTIMATE,PLANSTARTDATE,PLANFINISHDATE,FACTSTARTDATE,FACTFINISHDATE,";
    $sql.= "FINISHPERCENT,FINISHINFO,LIMITTYPE,LIMITDATE,TASKTYPE,ISMILESTONE,CREATEDATE) ";
    $sql.= "VALUES('".$sUserID."','".$sUsername."','".$sProjectID."','".$sOrderNumb."','".$sNewParentID."','";
    $sql.= $sWbsCode."','".$sTaskName."','".$sTaskInfo."','".$sPriority."','".$sDuration."','".$sIsEstimate."','";
    $sql.= $sPlanStartDate."','".$sPlanFinishDate."','".$sFactStartDate."','".$sFactFinishDate."','".$sFinishPercent."','";
    $sql.= $sFinishInfo."','".$sLimitType."','".$sLimitDate."','".$sTaskType."','".$sIsMilestone."',now())";
    $DB->tquery($sql,$conn);
    $sTaskID = mysql_insert_id();
    $DB->tcommit($conn);
    $sResult = "新任务添加成功";
    echo $json->encode(array('success'=>true,'msg'=>$sResult,'taskID'=>$sTaskID,'orderNumb'=>$sOrderNumb));
} 
else if ($sAction == ACTION_DELETE) {
    $sTaskID = fn_getParam('strTaskID',0);
    $sOrderNumb = fn_getParam("strOrderNumb","");
    $DB->tbegin($conn);
    $sResult = "您选择的任务删除失败";

    $sql = "UPDATE $TABLE_TASK SET ORDERNUMB=ORDERNUMB - 1 ";
    $sql.= "WHERE PROJECTID='".$sProjectID."' AND ORDERNUMB>'".$sOrderNumb."'";
    $DB->tquery($sql,$conn);
    $sql = "DELETE FROM $TABLE_TASK WHERE ID='".$sTaskID."'";
    $DB->tquery($sql,$conn);
    $sql = "DELETE FROM $TABLE_PRETASK WHERE TASKID='".$sTaskID."'";
    $DB->tquery($sql,$conn);
    $sql = "DELETE FROM $TABLE_TASK_RESOURCE WHERE TASKID='".$sTaskID."'";
    $DB->tquery($sql,$conn);

    $DB->tcommit($conn);
    $sResult = "您选择的任务删除成功";
} 
else if ($sAction == "moveup") {
    $sTaskID = fn_getParam('strTaskID',0);
    $sPrevTaskID = fn_getParam('strPrevTaskID',0);
    $sOrderNumb = fn_getParam('strOrderNumb',0);

    $DB->tbegin($conn);
    $sResult = "您选择的任务向上移动失败";
    $sql = "UPDATE $TABLE_TASK SET ORDERNUMB='".$sOrderNumb."' ";
    $sql.= "WHERE ID='".$sPrevTaskID."'";
    $DB->tquery($sql,$conn);
    $sql = "UPDATE $TABLE_TASK SET ORDERNUMB='".($sOrderNumb-1)."' ";
    $sql.= "WHERE ID='".$sTaskID."'";
    $DB->tquery($sql,$conn);
    $DB->tcommit($conn);
    $sResult = "您选择的任务向上移动成功";
} 
else if ($sAction == "movedn") {
    $sTaskID = fn_getParam('strTaskID',0);
    $sNextTaskID = fn_getParam('strNextTaskID',0);
    $sOrderNumb = fn_getParam('strOrderNumb',0);

    $DB->tbegin($conn);
    $sResult = "您选择的任务向下移动失败";
    $sql = "UPDATE $TABLE_TASK SET ORDERNUMB='".$sOrderNumb."' ";
    $sql.= "WHERE ID='".$sNextTaskID."'";
    $DB->tquery($sql,$conn);
    $sql = "UPDATE $TABLE_TASK SET ORDERNUMB='".($sOrderNumb+1)."' ";
    $sql.= "WHERE ID='".$sTaskID."'";
    $DB->tquery($sql,$conn);
    $DB->tcommit($conn);
    $sResult = "您选择的任务向下移动成功";
} 
else if ($sAction == "upgrade") {
    $sTaskID = fn_getParam('strTaskID',"0");
    $sNewParentID = fn_getParam('strNewParentID',"0");
    $sParentID  = fn_getParam('strParentID',"0");
    $sOrderNumb = fn_getParam('strOrderNumb',"0"); //等于0说明后面没有同一级的任务

    $DB->tbegin($conn);
    $sResult = "您选择的任务升级失败";
    $sql = "UPDATE $TABLE_TASK SET PARENTID='".$sNewParentID."' ";
    $sql.= "WHERE ID='".$sTaskID."'";
    $DB->tquery($sql,$conn);
    if ($sOrderNumb!="0") {
        $sql = "UPDATE $TABLE_TASK SET PARENTID='".$sTaskID."' ";
        $sql.= "WHERE PROJECTID='".$sProjectID."' AND PARENTID='".$sParentID."' AND ORDERNUMB>='".$sOrderNumb."'";
        $DB->tquery($sql,$conn);
    }
    $DB->tcommit($conn);
    $sResult = "您选择的任务升级成功";
} 
else if ($sAction == "downgrade") {
    $sTaskID = fn_getParam('strTaskID',"0");
    $sNewParentID = fn_getParam('strNewParentID',"0");

    $DB->tbegin($conn);
    $sResult = "您选择的任务降级失败";
    $sql = "UPDATE $TABLE_TASK SET PARENTID='".$sNewParentID."' ";
    $sql.= "WHERE ID='".$sTaskID."'";
    $DB->tquery($sql,$conn);
    $DB->tcommit($conn);
    $sResult = "您选择的任务降级成功";
} 

if ($sAction == "copy" || $sAction == "cut") {
    $sTaskID = fn_getParam('strTaskID',0);
    $sNewTaskID = fn_getParam('strNewTaskID',0);
    $sNewTaskIsFolder = fn_getParam('strIsFolder',0);

    $sResult = "任务粘帖失败";
    $sql = "SELECT ORDERNUMB,PARENTID FROM $TABLE_TASK WHERE ID='".$sNewTaskID."'";
    $rset = $DB->query($sql);
    if ($row = $DB->fetchRow($rset)) {
        $sNewOrderNumb = $row["ORDERNUMB"] + 1;
        $sNewParentID  = $row["PARENTID"];
        $DB->freeResult($rset);
    } else {
        echo $sResult;
        return;
    }
    if ($sNewTaskIsFolder == "1") { //is folder
        $sNewParentID = $sNewTaskID;
    }
}
if ($sAction == "copy") {
    $DB->tbegin($conn);
    $sql = "UPDATE $TABLE_TASK SET ORDERNUMB=ORDERNUMB + 1 ";
    $sql.= "WHERE PROJECTID='".$sProjectID."' AND ORDERNUMB>='".$sNewOrderNumb."'";
    $DB->tquery($sql,$conn);

    $sql = "INSERT INTO $TABLE_TASK(USERID,USERNAME,PROJECTID,ORDERNUMB,PARENTID,WBSCODE,TASKNAME,";
    $sql.= "TASKINFO,PRIORITY,DURATION,PLANSTARTDATE,PLANFINISHDATE,FACTSTARTDATE,FACTFINISHDATE,";
    $sql.= "FINISHPERCENT,FINISHINFO,LIMITTYPE,LIMITDATE,TASKTYPE,ISMILESTONE,CREATEDATE) ";
    $sql.= "SELECT USERID,USERNAME,PROJECTID,'".$sNewOrderNumb."','".$sNewParentID."',WBSCODE,TASKNAME,";
    $sql.= "TASKINFO,PRIORITY,DURATION,PLANSTARTDATE,PLANFINISHDATE,FACTSTARTDATE,FACTFINISHDATE,";
    $sql.= "FINISHPERCENT,FINISHINFO,LIMITTYPE,LIMITDATE,TASKTYPE,ISMILESTONE,NOW() ";
    $sql.= "FROM $TABLE_TASK ";
    $sql.= "WHERE ID='".$sTaskID."'";
    $DB->tquery($sql,$conn);

    $DB->tcommit($conn);
    $sResult = "您复制的任务粘帖成功";
    echo $sResult;
} 
else if ($sAction == "cut") {
    $DB->tbegin($conn);
    $sResult = "您剪切的任务粘帖失败";
    $sql = "SELECT ORDERNUMB FROM $TABLE_TASK WHERE ID='".$sTaskID."'";
    $sOrderNumb = $DB->getFirstField($sql); //the ordernumb of the cut task

    if ($sOrderNumb < $sNewOrderNumb) { //move to the position where after the cut task
        $sql = "UPDATE $TABLE_TASK SET ORDERNUMB=ORDERNUMB - 1 ";
        $sql.= "WHERE PROJECTID='".$sProjectID."' ";
        $sql.= "AND ORDERNUMB<'".$sNewOrderNumb."' AND ORDERNUMB>'".$sOrderNumb."'";
        $DB->tquery($sql,$conn);
    } else if ($sOrderNumb > $sNewOrderNumb) { //move to the position where before the cut task
        $sql = "UPDATE $TABLE_TASK SET ORDERNUMB=ORDERNUMB + 1 ";
        $sql.= "WHERE PROJECTID='".$sProjectID."' ";
        $sql.= "AND ORDERNUMB>'".$sNewOrderNumb."' AND ORDERNUMB<'".$sOrderNumb."'";
        $DB->tquery($sql,$conn);
    }
    $sql = "UPDATE $TABLE_TASK SET ORDERNUMB='".$sNewOrderNumb."',PARENTID='".$sNewParentID."' ";
    $sql.= "WHERE ID='".$sTaskID."'";
    $DB->tquery($sql,$conn);

    $DB->tcommit($conn);
    $sResult = "您剪切的任务粘帖成功";
    echo $sResult;
}

if ($sAction=="addPreTask" || $sAction=="updatePreTask" || $sAction=="deletePreTask" || $sAction=="batchDelPreTask") {
    $sPreID       = fn_getParam('strPreID','');
    $sPreIDs      = fn_getParam('strPreIDs','');
    $sTaskID      = fn_getParam('strTaskID','');
    $sPreTaskID   = fn_getParam('strPreTaskID','');
    $sPreTaskType = fn_getParam('strPreTaskType','');
    $sStayTime    = fn_getParam('strStayTime','0');
    $sPreRemark   = fn_getParam('strPreRemark','');

    $DB->tbegin($conn);
    if ($sAction == "addPreTask") {
        $sResult = "前置任务添加失败";
        /****方法迁移到 checkPreTask.php文件, 2009-06-03 ****
        $sql = "SELECT COUNT(1) FROM $TABLE_PRETASK ";
        $sql.= "WHERE TASKID='".$sTaskID."' AND PreTaskID='".$sPreTaskID."'";
        if ($DB->getFirstField($sql) == 0) {
            $sql = "INSERT INTO $TABLE_PRETASK(TASKID,PRETASKID,PRETASKTYPE,STAYTIME,REMARK) ";
            $sql.= "VALUES('".$sTaskID."','".$sPreTaskID."','".$sPreTaskType."','".$sStayTime."','".$sPreRemark."')";
            $DB->tquery($sql,$conn);
            $sResult = "前置任务添加成功";
        } else {
            $DB->trollback($conn);
            echo $json->encode(array('success'=>false,'msg'=>"该前置任务已经存在，不能重复添加"));
            return;
        }
        ****/
    } else if ($sAction == "updatePreTask") {
        $sResult = "前置任务更改失败";

        $sql = "UPDATE $TABLE_PRETASK SET ";
        $sql.= "PRETASKTYPE='".$sPreTaskType."',STAYTIME=".$sStayTime.",REMARK='".$sPreRemark."' ";
        $sql.= "WHERE ID='".$sPreID."'";
        $DB->tquery($sql,$conn);
        $sResult = "前置任务更改成功";
    } else if ($sAction == "deletePreTask") {
        $sResult = "前置任务删除失败";
        $sql = "DELETE FROM $TABLE_PRETASK WHERE ID='".$sPreID."'";
        $DB->tquery($sql,$conn);
        $sResult = "前置任务删除成功";
    } else if ($sAction=="batchDelPreTask") {
        $sResult = "批量删除数据失败";
        $sql = "DELETE FROM $TABLE_PRETASK WHERE ID IN (".$sPreIDs.")";
        $DB->tquery($sql,$conn);
        $sResult = "批量删除数据成功";    
    }
    $DB->tcommit($conn);
    echo $json->encode(array('success'=>true,'msg'=>$sResult));
}
if ($sAction=="addRes" || $sAction=="updateRes" || $sAction=="deleteRes" || $sAction=="batchDelRes") {
    $sResID  = fn_getParam('strResID','');
    $sResIDS = fn_getParam('strResIDS','');
    $sTaskID = fn_getParam('strTaskID','');
    $sResName    = fn_getParam('strResName','');
    $sTrueName   = fn_getParam('strTruename','');
    $sUsePercent = fn_getParam('strUsePercent','');
    $sResRemark  = fn_getParam('strResRemark','');

    $DB->tbegin($conn);
    if ($sAction == "addRes") {
        $sResult = "资源添加失败";
        $existCount = 0;
        if ($sResName != 'task_res_other') {
            $sql = "SELECT COUNT(1) FROM $TABLE_TASK_RESOURCE ";
            $sql.= "WHERE TASKID='".$sTaskID."' AND USERNAME='".$sResName."'";
            $existCount = $DB->getFirstField($sql);
        }
        if ($existCount == 0) {
            $sql = "INSERT INTO $TABLE_TASK_RESOURCE(TASKID,USERNAME,TRUENAME,USAGEUNIT,REMARK) ";
            $sql.= "VALUES('".$sTaskID."','".$sResName."','".$sTrueName."','".$sUsePercent."','".$sResRemark."')";
            $DB->tquery($sql,$conn);
            $sResult = "资源添加成功";
        } else {
            $DB->trollback($conn);
            echo $json->encode(array('success'=>false,'msg'=>"该资源已经存在，不能重复添加"));
            return;
        }
    } else if ($sAction == "updateRes") {
        $sResult = "资源更改失败";

        $sql = "UPDATE $TABLE_TASK_RESOURCE SET ";
        $sql.= "USERNAME='".$sResName."',TRUENAME='".$sTrueName."',USAGEUNIT='".$sUsePercent."',REMARK='".$sResRemark."' ";
        $sql.= "WHERE ID='".$sResID."'";
        $DB->tquery($sql,$conn);
        $sResult = "资源更改成功";
    } else if ($sAction == "deleteRes") {
        $sResult = "资源删除失败";
        $sql = "DELETE FROM $TABLE_TASK_RESOURCE WHERE ID='".$sResID."'";
        $DB->tquery($sql,$conn);
        $sResult = "资源删除成功";
    } else if ($sAction=="batchDelRes") {
        $sResult = "批量删除数据失败";
        $sql = "DELETE FROM $TABLE_TASK_RESOURCE WHERE ID IN (".$sResIDS.")";
        $DB->tquery($sql,$conn);
        $sResult = "批量删除数据成功";    
    }
    $DB->tcommit($conn);
    echo $json->encode(array('success'=>true,'msg'=>$sResult));
}
?>